<?php
/**
 * Core_Controller_Plugin_DbProfiler
 * 
 * @author naspeh
 * @version $Id: DbProfiler.php 658 2008-06-13 14:24:51Z naspeh $
 */
class Core_Controller_Plugin_DbProfiler extends Zend_Controller_Plugin_Abstract
{
    /**
     * Запускается после цикла диспетчеризации
     *
     */
    public function dispatchLoopShutdown () {
        $profiler = Core_Db::getInstance()->getProfiler();
        $totalTime = $profiler->getTotalElapsedSecs();
        $queryCount = $profiler->getTotalNumQueries();
        $queries = array();
        $longestTime = 0;
        $longestQuery = null;
        $format = '"%s" # %s';
        if (array() != ($queryProfiles = $profiler->getQueryProfiles())) {
            foreach ($queryProfiles as $query) {
                $queries[] = sprintf($format, $query->getQuery(), $query->getElapsedSecs()); 
                if ($query->getElapsedSecs() > $longestTime) {
                    $longestTime = $query->getElapsedSecs();
                    $longestQuery = $query->getQuery() ;
                }
            }
        }
        $vars['totalTime']  = $totalTime;
        $vars['queryCount'] = $queryCount;
        $vars['longest']    = sprintf($format, $longestQuery, $longestTime);
        $vars['queries']    = $queries;
        Core_Log::dump($vars, 'Profiler for DB');
    }    
}